﻿using MaterialDesignThemes.Wpf;
using SharpSoft.WPF.Masks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace SharpSoft.WPF
{
    public static class DialogHelper
    {
        static DialogHelper()
        {


        }

        public static void InitMaterial()
        {
            bool found = false;
            foreach (var item in Application.Current.Resources.MergedDictionaries)
            {
                if (item is BundledTheme)
                {
                    found = true;
                    break;
                }
            }
            if (!found)
            {
                var bt = new BundledTheme()
                {
                    PrimaryColor = MaterialDesignColors.PrimaryColor.DeepPurple,
                    SecondaryColor = MaterialDesignColors.SecondaryColor.Lime,
                    ColorAdjustment = new ColorAdjustment()
                };
                Application.Current.Resources.MergedDictionaries.Add(bt);
                Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml") });
            }

        }

        /// <summary>
        /// 弹出普通对话框
        /// </summary>
        /// <param name="uielement"></param>
        /// <param name="text"></param>
        /// <param name="color"></param>
        /// <returns></returns>
        public static async Task<SampleDialog> SampleAsync(this UIElement uielement, string text, ThemeColor color = ThemeColor.None, TimeSpan? dur = null)
        {
            SampleDialog sampleDialog = new SampleDialog() { Text = text, TextBrush = GetThemeBrush(color) };
            var mask = await uielement.MaskAsync(sampleDialog);
            if (dur.HasValue)
            {
                planClose(sampleDialog, dur.Value);
            }
            return sampleDialog;
        }
        /// <summary>
        /// 计划关闭
        /// </summary>
        /// <param name="sampleDialog"></param>
        private static async void planClose(SampleDialog sampleDialog, TimeSpan dur)
        {
            await Task.Delay(dur);
            sampleDialog.Close();
        }
        /// <summary>
        /// 弹出确认对话框
        /// </summary>
        /// <param name="uielement"></param>
        /// <param name="text"></param>
        /// <param name="confirmText"></param>
        /// <param name="declineText"></param>
        /// <returns></returns>
        public static async Task<ConfirmDialog> ConfirmAsync(this UIElement uielement, string text, string confirmText = "确认", string declineText = "取消")
        {
            ConfirmDialog confirmDialog = new ConfirmDialog() { Text = text, ConfirmText = confirmText, DeclineText = declineText };
            var mask = await uielement.MaskAsync(confirmDialog);
            return confirmDialog;
        }
        /// <summary>
        /// 弹出耗时等待框
        /// </summary>
        /// <param name="uielement"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        public static async Task<WaittingDialog> WaitAsync(this UIElement uielement, string text)
        {
            WaittingDialog waitDialog = new WaittingDialog() { Text = text };
            var mask = await uielement.MaskAsync(waitDialog);
            return waitDialog;
        }



        public static Brush GetThemeBrush(ThemeColor color)
        {
            switch (color)
            {
                case ThemeColor.Warnning:
                    return Brushes.Gold;
                case ThemeColor.Error:
                    return Brushes.OrangeRed;
                case ThemeColor.Success:
                    return Brushes.ForestGreen;
                case ThemeColor.None:
                case ThemeColor.Info:
                default:
                    return Brushes.Black;
            }
        }
    }
    public enum ThemeColor
    {
        None,
        Info,
        Warnning,
        Error,
        Success
    }
}
